<?php

/**
 * Created by PhpStorm daichen.
 * User: 橙橙同学
 * Date: 2017/11/22
 * Time: 11:00
 */
namespace app;

class MsbhApiTest
{
    const APPKEY = 'NmE0MmM2ZjZmODdmMGImYzU4Yjg1UYU2MWE=';
    const HOST = 'http://113.140.27.50:8740/CRMInterface.ashx';

    protected static function getSign()
    {
        $ts = time();
        $signStr = $ts.self::APPKEY;
        return array(
            'ts'=>$ts,
            'sig'=>md5($signStr)
        );
    }

    protected static function linUrl($name)
    {
        return self::HOST.'/Msbh/'.$name;
    }

    public static function __callStatic($name,$arguments)
    {
        $postData = self::generatePostData($arguments[0]);

        $url = self::linUrl($name);
        //请求
        $res = http_post($url,$postData);

        //如果返回false  http请求失败
        if(!$res)
        {
            $res = json_encode(array('msg'=>'HTTP请求错误'));
        }
        self::MarkResponseData($url,$postData,$res);
    }

    protected static function commonReturn($errCode,$msg)
    {
        echo "<p style='background: #d9edf7;margin-bottom: 1px;padding: 10px;border-radius: 5px'>返回码 {$errCode} 返回信息 {$msg}</p>";
    }

    protected static function generatePostData($args)
    {
        $postData = $args;
        return json_encode($postData);
    }

    protected static function MarkResponseData($url,$postData,$data)
    {
        $insertRes = pdo_insert('api_record',array(
                'api'=>$url,
                'request'=>$postData,
                'response'=>$data,
                'create_time'=>time(),
                'apitype'=>'msbh',
                'request_type'=>'post'
            )
        );
        if($insertRes)
        {
            self::commonReturn(0,'接口'.$url.' 测试结果记录成功');
        }
        else{
            self::commonReturn(-1,'接口'.$url.' 测试结果记录失败');
        }
    }

    /**
     * @return array
     * @author:橙橙同学
     * @func:根据会员ID获取券列表，Post方式访问
     */
    public static function dataGetCouponList()
    {
        $args = array(
            //店铺代码（例：MSJT01）
            'code'=>'',
            'cardID'=>'',
            //柜组id,多个用英文逗号隔开
            'gzID'=>'',
        );
        return $args;
    }

    /**
     * @return array
     * @author:橙橙同学
     * @func:核销，Post方式访问
     */
    public static function dataConsumecouponHx()
    {
        $args = array(
            'code'=>1,
            'cardID'=>'user_id',
            'ticket'=>'610429199503101719',
            'type'=>1,
        );
        return $args;
    }

    /**
     * @return array
     * @author:橙橙同学
     * @func:撤销
     */
    public static function dataConsumecouponCx()
    {

        $args = array(
            'code'=>'',
            'cardID'=>'',
            'ticket'=>'',
            'type'=>2,
        );
        return $args;
    }

}